#pragma once


#include "stdafx.h"
#include "resource.h"

//extern "C"{
#include <iostream>
//};
#include <string>
#include "atlcoll.h"

#include "TcpClient.h"
#include "StringFunctions.h"

using namespace std;



struct menuItem
{
public:
	string text;
	bool enabled;

	menuItem::menuItem(string strMenu)
	{
		if(strMenu.substr(0,1) == "_")
		{
			text=strMenu.substr(1,strMenu.npos-1);
			enabled=false;
		}
		else
		{
			text=strMenu;
			enabled=true;
		}
	}
};

class menuItemTraits : public CElementTraits< menuItem >
{
public:
    // Override the comparison to only compare
    // the text value.

    static bool CompareElements( const menuItem& element1, const menuItem& element2)
    {
        if ( element1.text == element2.text )
            return true;
        else
            return false;
    };
};



class FileInfo
{

public:
	CComBSTR FileName;
	CComBSTR State;
	CComBSTR Status;
	CComBSTR LocalVersion;
	CComBSTR ServerVersion;
	CComBSTR OverlayType;
	CComBSTR LockStatus;
	CComBSTR LockOwner;
	CComBSTR OtherPends;
	CComBSTR OtherUsers;

	void SetData(CComBSTR FileInfostr)
	{
		 FileName = "";
		 State = "";
		 Status = "";
		 LocalVersion = "";
		 ServerVersion = "";
		 OverlayType = "";
		 LockStatus = "";
		 LockOwner = "";
		 OtherPends = "";
		 OtherUsers = "";
		size_t cutAt;
			int i=0;
			string str = CComBSTRtoString(FileInfostr);
			while( (cutAt =  str.find_first_of("|")) != str.npos )
			{
				if(cutAt > 0)
				{
					CComBSTR info (str.substr(0,cutAt).c_str());
					switch (i)
					{
					case 0:
						{
							FileName.Append(info);
							break;
						}
					case 1:
						{
							State.Append(info);
							break;
						}
					case 2:
						{
							Status.Append(info);
							break;
						}
					case 3:
						{
							LocalVersion.Append(info);
							break;
						}
					case 4:
						{
							ServerVersion.Append(info);
							break;
						}
					case 5:
						{
							OverlayType.Append(info);
							break;
						}
					case 6:
						{
							LockStatus.Append(info);
							break;
						}
					case 7:
						{
							LockOwner.Append(info);
							break;
						}
					case 8:
						{
							OtherPends.Append(info);
							break;
						}
					}
				}
				str = str.substr(cutAt+1);
				i++;
			}
			if(str.length() > 0)
			{
				CComBSTR info (str.substr(0,cutAt).c_str());
				OtherUsers.Append(info);
			}

	}

};

class FileInfos
{
public:

	CAtlMap<CComBSTR, FileInfo*,CElementTraits<CComBSTR>,CDefaultElementTraits<FileInfo*>> mapFileInfo;
	CComBSTR lastFileInfoPath;

	void SetData(CComBSTR fileInfosStr)
	{
		size_t cutAt;
		int i=0;
		string str = CComBSTRtoString(fileInfosStr);

		mapFileInfo.RemoveAll();

	//	while (mapFileInfo.GetCount()>0)
	//	{
	////		delete (mapFileInfo.GetValueAt(0));
	//		mapFileInfo.RemoveAtPos(0);
	//	}

		while( (cutAt = str.find_first_of("`")) != str.npos )
		{
			if(cutAt > 0)
			{
				try
				{
					CComBSTR infoStr (str.substr(0,cutAt).c_str());
					FileInfo* fi = new FileInfo();
					fi->SetData(infoStr);
					string* k = new string;
					*k =	CComBSTRtoString(fi->FileName);
					mapFileInfo.SetAt(k->c_str(), fi);
				}
				catch (...)
				{
					// just let it fail
				}
			}
			str = str.substr(cutAt+1);
		}
		if(str.length() > 0)
		{
				try
				{
					CComBSTR infoStr (str.c_str());
					FileInfo* fi = new FileInfo();
					fi->SetData(infoStr);
					string k = CComBSTRtoString(fi->FileName);
					mapFileInfo.SetAt(k.c_str(), fi);
				}
				catch (...)
				{
					// just let it fail
				}

		}
	}
	

};

class CLizardClient
{
public:
	CAtlList <menuItem, menuItemTraits> items;

	CAtlList <menuItem, menuItemTraits>* CLizardClient::GetList()
	{
		return &items;
	}

	void CLizardClient::GetItemsForPaths(string paths)
	{
		items.RemoveAll();
		string strReq = "GETMENUS|";
		strReq += paths;

	

		string strResp = callserver(strReq);
		
		if (strResp.length() ==0)
			return;

		string str=strResp;
		{
			size_t cutAt;
			while( (cutAt = str.find_first_of("|")) != str.npos )
			{
				if(cutAt > 0)
				{
					menuItem mi (str.substr(0,cutAt));
					items.AddTail(mi);
				}
				str = str.substr(cutAt+1);
			}
			if(str.length() > 0)
			{
				menuItem mi (str);
				items.AddTail(mi);
			}
		}
	}

	
};

